/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ocr.idcard;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import com.yylending.las.R;


public final class ViewfinderView extends View {
	static final String TAG = "ViewfinderView";
	private final static long ANIMATION_DELAY = 10L;
	private int mLineStep = 0;
	private Rect mLineRect, mFrontRect, mBackRect;
	private Drawable mLineDrawable, mFrontDrawable, mBackDrawable;
	private int LINE_HEIGHT = 0;
	private int LINE_SPEED = 5;
	private final Paint mPaint;
	private final int mFrameColor;
	private int mTipColor;
	private float mTipTextSize;
	private String mTipText;
	private int mLineWidth = 4;
	private int mRoundWidth = 38;
	private int mFrontPaddingRight = 60;
	private int mFrontPaddingTop = 60;
	private int mBackPaddingLeft = 60;
	private int mBackPaddingTop = 60;

	private boolean mDown = true;
	private int mFrontImgHeight = 0;
	private int mFrontImgWidth = 0;
	private int mBackImgHeight = 0;
	private int mBackImgWidth = 0;

	private boolean mIsFront = true;

	public ViewfinderView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setWillNotDraw(false);
		mPaint = new Paint();
		Resources resources = getResources();
		mFrameColor = resources.getColor(R.color.color_4b7cf9);
		mTipColor = resources.getColor(R.color.color_ffffff);
		mTipTextSize = getResources().getDisplayMetrics().density * 15;
		mLineWidth = (int)(getResources().getDisplayMetrics().density * 2);
		mRoundWidth = (int)(getResources().getDisplayMetrics().density * 19);
		mFrontPaddingRight = (int)(getResources().getDisplayMetrics().density * 39);
		mFrontPaddingTop = (int)(getResources().getDisplayMetrics().density * 41);
		mBackPaddingLeft = (int)(getResources().getDisplayMetrics().density * 22);
		mBackPaddingTop = (int)(getResources().getDisplayMetrics().density * 26);

		mLineRect = new Rect();
		int lineDrawableId = R.drawable.scan_line_landscape;
		mLineDrawable = context.getResources().getDrawable(lineDrawableId);
		LINE_HEIGHT = mLineDrawable.getIntrinsicHeight();
		int frontDrawableId = R.drawable.id_card_head_border;
		mFrontDrawable = context.getResources().getDrawable(frontDrawableId);
		mFrontImgHeight = mFrontDrawable.getIntrinsicHeight();
		mFrontImgWidth = mFrontDrawable.getIntrinsicWidth();
		mFrontRect = new Rect();

		int backDrawableId = R.drawable.id_card_back_border;
		mBackDrawable = context.getResources().getDrawable(backDrawableId);
		mBackImgHeight = mBackDrawable.getIntrinsicHeight();
		mBackImgWidth = mBackDrawable.getIntrinsicWidth();
		mBackRect = new Rect();
	}

	@Override
	public void onDraw(Canvas canvas) {
		int width = canvas.getWidth();
		int height = canvas.getHeight();

		if(mIsFront){
			canvas.save();
			mFrontRect.set(width - mFrontImgWidth - mFrontPaddingRight, mFrontPaddingTop, width - mFrontPaddingRight, mFrontPaddingTop + mFrontImgHeight);
			mFrontDrawable.setBounds(mFrontRect);
			mFrontDrawable.draw(canvas);
			canvas.restore();
		}else{
			canvas.save();
			mBackRect.set(mBackPaddingLeft, mBackPaddingTop, mBackPaddingLeft + mBackImgWidth, mBackPaddingTop + mBackImgHeight);
			mBackDrawable.setBounds(mBackRect);
			mBackDrawable.draw(canvas);
			canvas.restore();
		}

		if(mDown){
			if ((mLineStep += LINE_SPEED) < height - LINE_HEIGHT) {
				canvas.save();
				mLineRect.set(0, mLineStep, width, mLineStep + LINE_HEIGHT);
				mLineDrawable.setBounds(mLineRect);
				mLineDrawable.draw(canvas);
				canvas.restore();
			} else {
				mDown = false;
			}
		}else{
			if ((mLineStep -= LINE_SPEED) > LINE_HEIGHT) {
				canvas.save();
				mLineRect.set(0, mLineStep - LINE_HEIGHT, width, mLineStep);
				mLineDrawable.setBounds(mLineRect);
				mLineDrawable.draw(canvas);
				canvas.restore();
			} else {
				mDown = true;
			}
		}

		canvas.save();
		mPaint.setColor(mFrameColor);
		canvas.drawRect(0, 0, mRoundWidth, mLineWidth, mPaint);
		canvas.drawRect(0, 0, mLineWidth, mRoundWidth, mPaint);
		canvas.drawRect(width - mRoundWidth, 0, width, mLineWidth, mPaint);
		canvas.drawRect(width - mLineWidth, 0, width, mRoundWidth, mPaint);
		canvas.drawRect(0, height - mLineWidth, mRoundWidth, height, mPaint);
		canvas.drawRect(0, height - mRoundWidth, mLineWidth, height, mPaint);
		canvas.drawRect(width - mRoundWidth, height - mLineWidth, width, height, mPaint);
		canvas.drawRect(width - mLineWidth, height - mRoundWidth, width, height, mPaint);
		//绘制提示文字
		if (!TextUtils.isEmpty(mTipText)) {
			mPaint.setTextAlign(Paint.Align.CENTER);
			mPaint.setColor(mTipColor);
			mPaint.setTextSize(mTipTextSize);
			//canvas.translate(width / 2, frame.top + frame.height() / 10);
			canvas.drawText(mTipText, 0, 0, mPaint);
		}
		canvas.restore();
		postInvalidateDelayed(ANIMATION_DELAY, 0, 0, width, height);
	}

	public void setTipText(String str) {
		this.mTipText = str;
	}

	public void setTipColor(int color) {
		this.mTipColor = color;
	}

	public boolean isFront() {
		return mIsFront;
	}

	public void setFront(boolean front) {
		mIsFront = front;
	}
}